package conson.tech.manage.coupons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.exception.BaseException;
import conson.tech.core.response.Response;
import conson.tech.manage.common.constant.BusinessMessage;
import conson.tech.manage.coupons.entity.dto.CouponsGrantDto;
import conson.tech.manage.coupons.entity.pojo.CouponsBatch;
import conson.tech.manage.coupons.entity.pojo.CouponsUsers;
import conson.tech.manage.coupons.entity.pojo.CouponsUsersLog;
import conson.tech.manage.coupons.entity.vo.CouponsGrantInformationVo;
import conson.tech.manage.coupons.entity.vo.CouponsGrantLogVo;
import conson.tech.manage.coupons.entity.vo.CouponsGrantVo;
import conson.tech.manage.coupons.mapper.CouponsBatchMapper;
import conson.tech.manage.coupons.mapper.CouponsUsersLogMapper;
import conson.tech.manage.coupons.mapper.CouponsUsersMapper;
import conson.tech.manage.coupons.service.CouponsGrantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: CouponsServiceImpl
 * Description:
 * Author: ZYF
 * Date: 2020/7/21
 * Version: 1.0
 */
@Slf4j
@Service
public class CouponsGrantServiceImpl implements CouponsGrantService {


    @Autowired
    private CouponsUsersMapper couponsUsersMapper;

    @Autowired
    private CouponsUsersLogMapper couponsUsersLogMapper;

    @Autowired
    private CouponsBatchMapper couponsBatchMapper;


    /**
     * 查询发放优惠券列表
     *
     * @param pageIndex
     * @param pageSize
     * @dto 查询入参
     */
    @Override
    public Page<CouponsGrantVo> queryList(Integer pageIndex, Integer pageSize, CouponsGrantDto dto) {

        log.info("开始查询优惠券列表信息");
        //构建分页查询条件
        Page<CouponsGrantVo> page = new Page<>(pageIndex, pageSize);

        page = this.couponsUsersMapper.queryList(page, dto);

        List<CouponsGrantVo> collect = page.getRecords().stream().map(couponsUser -> {
            CouponsGrantVo couponsGrantVo = new CouponsGrantVo();
            couponsGrantVo.setId(couponsUser.getId());
            couponsGrantVo.setCouponsCode(couponsUser.getCouponsCode());
            couponsGrantVo.setState(couponsUser.getState());
            couponsGrantVo.setBeginTime(couponsUser.getBeginTime());
            CouponsBatch couponsBatch = this.couponsBatchMapper.selectById(couponsUser.getBatchId());
            couponsGrantVo.setBatchMoney(couponsBatch.getBatchMoney());
            couponsGrantVo.setBatchType(couponsBatch.getBatchType());
            couponsGrantVo.setEndTime(couponsBatch.getEndTime());
            couponsGrantVo.setStartTime(couponsBatch.getStartTime());
            couponsGrantVo.setLimitMoney(couponsBatch.getLimitMoney());
            couponsGrantVo.setBatchId(couponsBatch.getId());
            return couponsGrantVo;
        }).collect(Collectors.toList());

        page.setRecords(collect);

        log.info("结束查询优惠券列表信息");
        return page;
    }

    /**
     * 作废优惠券
     *
     * @param couponsCode 优惠券券码
     * @param userName    用户名称
     * @param userId      用户id
     */
    @Override
    @Transactional
    public Response cancellationCoupons(String couponsCode, String userName, Long userId) {

        //根据优惠券券码查询优惠券信息
        CouponsUsers users = this.couponsUsersMapper.selectCouponsUser(couponsCode);
        if (users.getState() != 0) {
            throw new BaseException(BusinessMessage.Coupon_IS_NOT_VALID_ERROR);
        }
        users.setState(3);
        this.couponsUsersMapper.updateById(users);

        CouponsUsersLog logs = new CouponsUsersLog();
        logs.setCouponsId(users.getId());
        logs.setCouponsCode(couponsCode);
        logs.setOperateType(0);
        logs.setOperateDesc("优惠券作废");
        logs.setCouponsState(3);
        logs.setCreateTime(LocalDateTime.now());
        logs.setCreateBy(userId);
        logs.setCreateName(userName);
        this.couponsUsersLogMapper.insert(logs);

        return Response.success();

    }

    /**
     * 优惠券发放日志查询优惠券信息
     *
     * @param couponsCode 优惠券code码
     */
    @Override
    public CouponsGrantInformationVo cancellationCouponsInformation(String couponsCode) {

        CouponsGrantInformationVo couponsGrantInformationVo = new CouponsGrantInformationVo();

        CouponsUsers couponsUsers = this.couponsUsersMapper.selectOne(new QueryWrapper<CouponsUsers>().eq("coupons_code", couponsCode));

        couponsGrantInformationVo.setState(couponsUsers.getState());
        couponsGrantInformationVo.setStartTime(couponsUsers.getStartTime());
        couponsGrantInformationVo.setEndTime(couponsUsers.getEndTime());

        CouponsBatch couponsBatch = this.couponsBatchMapper.selectOne(new QueryWrapper<CouponsBatch>().eq("id", couponsUsers.getBatchId()));
        couponsGrantInformationVo.setBatchName(couponsBatch.getBatchName());
        couponsGrantInformationVo.setBatchType(couponsBatch.getBatchType());
        couponsGrantInformationVo.setBatchMoney(couponsBatch.getBatchMoney());
        couponsGrantInformationVo.setBatchDiscount(couponsBatch.getBatchDiscount());
        couponsGrantInformationVo.setLimitMoney(couponsBatch.getLimitMoney());
        couponsGrantInformationVo.setId(couponsBatch.getId());
        couponsGrantInformationVo.setUsedDes(couponsBatch.getUsedDes());
        return couponsGrantInformationVo;
    }

    /**
     * 查询优惠券全流程日志信息
     *
     * @param pageIndex
     * @param pageSize
     * @param couponsCode 优惠券code码
     */
    @Override
    public Page<CouponsGrantLogVo> cancellationCouponsLog(Integer pageIndex, Integer pageSize, String couponsCode) {

        log.info("开始查询优惠券全流程日志信息");
        //构建分页查询条件
        Page<CouponsGrantLogVo> page = new Page<>(pageIndex, pageSize);

        page = this.couponsUsersLogMapper.cancellationCouponsLog(page, couponsCode);

        log.info("结束查询优惠券全流程日志信息");
        return page;

    }
}
